﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Services
{
	using System.Collections.Generic;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Repositories;
	using Falcon.Core.Domain.Services;

	public class UserMessageAttachmentService : IUserMessageAttachmentService
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="UserMessageAttachmentService"/> class.
		/// </summary>
		/// <param name="attachmentStoreService">The attachment store service.</param>
		/// <param name="userMessageAttachmentRepository">The user message attachment repository.</param>
		public UserMessageAttachmentService(
			IAttachmentStoreService attachmentStoreService,
			IUserMessageAttachmentRepository userMessageAttachmentRepository)
		{
			this.AttachmentStoreService = attachmentStoreService;
			this.UserMessageAttachmentRepository = userMessageAttachmentRepository;
		}

		/// <summary>
		/// Gets or sets the attachment store service.
		/// </summary>
		/// <value>The attachment store service.</value>
		public IAttachmentStoreService AttachmentStoreService
		{
			get;
			private set;
		}

		/// <summary>
		/// Gets or sets the user message attachment repository.
		/// </summary>
		/// <value>The user message attachment repository.</value>
		public IUserMessageAttachmentRepository UserMessageAttachmentRepository
		{
			get; 
			private set;
		}

		/// <summary>
		/// Gets all the user message attachments by the specified specification.
		/// </summary>
		/// <param name="specification">The specification.</param>
		/// <returns></returns>
		public IList<UserMessageAttachment> GetAll(UserMessageAttachmentSpecification specification)
		{
			return this.UserMessageAttachmentRepository.GetAll(specification);
		}

		/// <summary>
		/// Creates the user message attachment.
		/// </summary>
		/// <returns></returns>
		public UserMessageAttachment Create()
		{
			return new UserMessageAttachment();
		}

		/// <summary>
		/// Inserts the user message attachment into the storage.
		/// </summary>
		/// <param name="relation">The relation.</param>
		public void Insert(UserMessageAttachment relation)
		{
			this.UserMessageAttachmentRepository.Insert(relation);
		}

		/// <summary>
		/// Inserts the user message attachments into the storage.
		/// </summary>
		/// <param name="message">The message.</param>
		/// <param name="attachments">The attachments.</param>
		public void Insert(UserMessage message, IList<AttachmentStore> attachments)
		{
			foreach (var store in attachments)
			{
				var relation = this.Create();

				relation.MessageId = message.Id;
				relation.Attachment = store.Attachment;

				this.Insert(relation);
				this.AttachmentStoreService.Delete(store, false);
			}
		}

		/// <summary>
		/// Deletes the specified user message attachment in the storage.
		/// </summary>
		/// <param name="relation">The <see cref="UserMessageAttachment"/>.</param>
		public void Delete(UserMessageAttachment relation)
		{
			this.UserMessageAttachmentRepository.Delete(relation);
		}
	}
}